#ifdef linux
#include<GL/glew.h>
#include<GLFW/glfw3.h>
#else
#include<glew.h>
#include<glfw3.h>
#endif
#include<iostream>
#include<time.h>
#include<math.h>

#include "myBase.h"

class myStateCalls
{
    public:
    static void enableDepth() { glEnable(GL_DEPTH_TEST); }
};
class initState
{
    public:
    //static void init
};

int main()
{
	int errorval = 0;	
	errorval = glfwInit();
    if(!errorval) return 1;
    
    myWindow *window = new myWindow(800,600, "Hello World");    
    
    window->createWindow();
    window->makeCurrent();
    	
    glewExperimental = GL_TRUE;
    glewInit();
	
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    float points[] = { 0.0f, 0.5f, 0.0f,
                       0.5f, -0.5f, 0.0f,
                       -0.5f, -0.5f, 0.0f
                     };

    GLuint vbo = 0;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);

    GLuint vao = 0;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 0, 0);


    myShader *vshader = new myShader(myShader::vertexShader, "Vertex Basic");
    myShader *pshader = new myShader(myShader::fragmentShader, "Fragment Basic");
    myShaderPipe *pipe = new myShaderPipe();

    vshader->createShaderFromFile("vertex2.glsl");
    pshader->createShaderFromFile("fragment2.glsl");
    vshader->compileShader();
    pshader->compileShader();
    
    pipe->createProgram();
    pipe->attachShader(vshader);
    pipe->attachShader(pshader);
    pipe->linkPipe();
    while(!glfwWindowShouldClose(window->getWindow()))
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram(pipe->getHandle());
        glBindVertexArray(vao);
        float times[4] = { sin(time(NULL)) * 0.5f + 0.5f, cos(time(NULL))*0.5f + 0.5f, 0,0};
        glVertexAttrib4fv(1, times);
   
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwPollEvents();
        glfwSwapBuffers(window->getWindow());
    }
    glfwTerminate();
	return 0;
}
